using System;
using System.Collections.Generic;
using System.Linq;
using Brouteforce.utilities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Runtime.InteropServices;
using Brouteforce.NewManagers;
using Brouteforce.Characters;


namespace Brouteforce {
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class BFGame : Microsoft.Xna.Framework.Game {
        public static GraphicsDeviceManager Graphics;
        public static SpriteBatch SpriteBatch;
        public static GameTime CurrentTime;
        public static BFGame Instance;
        public static Manager[] Managers = { InputManager.Instance, TransitionManager.Instance, UIControlManager.Instance  , ScreenManager.Instance};
        public static int LoopIndex = 0;
        #region DLLs Import

        [DllImport("user32.dll")]
        public static extern void ClipCursor(ref Rectangle rect);
        [DllImport("user32.dll")]
        public static extern void ClipCursor(IntPtr rect);

        #endregion

        #region Sprite Data

        public static Texture2D Blank;
        public static SpriteFont NormalFont;
        public static SpriteFont MessageFont;
        public static Texture2D TileGround;
        public static Texture2D TileTower;
        public static Texture2D TileShooter;
        public static Texture2D TileDefender;
        public static Texture2D TileSuicideBomber;
        public static Texture2D Player;
        public static Texture2D FootPrintsCursor;
        public static Texture2D ScopeCursor;
        public static Texture2D PointerCursor;
        public static Texture2D LineBorder;

        #endregion

        public BFGame() {
            Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Window.Title = "Brute-Force";
            Graphics.PreferredBackBufferWidth = 1280;
            Graphics.PreferredBackBufferHeight = 700;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize(){
            Instance = this;
            // TODO: Add your initialization logic here
            
            //TODO REMOVE
            //InputManager.Initialize();
            



            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent() {
            // Create a new SpriteBatch, which can be used to draw textures.
            SpriteBatch = new SpriteBatch(GraphicsDevice);

            
            //TileGround = Content.Load<Texture2D>("inna");
            TileGround = Content.Load<Texture2D>("TileGround");
            TileTower = Content.Load<Texture2D>("TileTower");
            FootPrintsCursor = Content.Load<Texture2D>("footprints");
            ScopeCursor = Content.Load<Texture2D>("scope");
            PointerCursor = Content.Load<Texture2D>("pointer");

            //ToDo  To Modify the tiles content
            TileShooter = Content.Load<Texture2D>("PlayerShooter");
            TileDefender = Content.Load<Texture2D>("PlayerDefender");
            TileSuicideBomber = Content.Load<Texture2D>("PlayerKamikaz");

            

            Blank = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            Blank.SetData(new[] { Color.White });

            NormalFont = Content.Load<SpriteFont>("myFont");
            MessageFont = Content.Load<SpriteFont>("MessageFont");
            LineBorder = Content.Load<Texture2D>("LineBorder");

            foreach (var manager in Managers) manager.LoadContent(Content);

            foreach (var manager in Managers) manager.Initialize();
            //GameManager.Initialize();


        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent() {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime){
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();
            CurrentTime = gameTime;

            //GameManager.Update();
            foreach (var manager in Managers) manager.Update(gameTime, this);

            
            base.Update(gameTime);
            LoopIndex++;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime) {
            CurrentTime = gameTime;
            GraphicsDevice.Clear(Color.Transparent);
            base.Draw(gameTime);

            SpriteBatch.Begin();

            foreach (var manager in Managers) manager.Draw(gameTime, this);

            //GameManager.Draw();

            Draws.DrawString(3,3,"FPS: " + (1 / gameTime.ElapsedGameTime.TotalSeconds));

            SpriteBatch.End();

        }
    }
}
